package com.ttbj.core.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cloopen.rest.sdk.CCPRestSDK;

public class SmsUtils {
	
	private static Logger logger = LoggerFactory.getLogger(SmsUtils.class);
	
	private static CCPRestSDK restAPI = new CCPRestSDK();
	private static String smsTemplateId = "24043";
	
	//【银湖网】恭喜您注册银湖网成功，并获得邀请码：{1},你可以将邀请码发送给朋友，邀他加入银湖网，朋友注册成功后您还可以获得{2}元红包。
	private static String smsReInviteTeId = "8789";
	
	//【银湖网】恭喜您获得邀请码：{1},你可以将邀请码发送给朋友，邀他加入银湖网，朋友注册成功后您还可以获得{2}元红包。
	private static String smsInviteTeId = "8788"; 

	public void init() throws Exception {
	//restAPI.init("sandboxapp.cloopen.com", "8883");// 初始化服务器地址和端口 (开发) Rest URL：
		restAPI.init("app.cloopen.com", "8883");// 初始化服务器地址和端口 (生产) Rest URL：
		restAPI.setAccount("aaf98fda461292a201461863fef004fc", "27b8dca0209b403eba8dce93b76b1c77");// 初始化主帐号名称和主帐号令牌
		restAPI.setAppId("aaf98fda461292a201461c8e5842079c");// 初始化应用ID
	}

	public static HashMap<String, Object> sendTemplateSms(ArrayList<String> mobileList, String smsTemplateId,
		String[] params) {
		HashMap<String, Object> rsltMap = restAPI.sendTemplateSMS(mobileList.toString().replaceAll("[\\]\\[\\s]", ""),
			smsTemplateId, params);
		return rsltMap;
	}

	public static HashMap<String, Object> sendVoiceVerify(String code, String mobile, String dispPhoneNo, String playCnt) {
		HashMap<String, Object> rsltMap = restAPI.voiceVerify(code, mobile, dispPhoneNo, playCnt, "");
		return rsltMap;
	}

	/**
	 * 
	 * @param mobileNo
	 * @param mobileCode
	 * @return
	 */
	public static boolean doSend(String mobileNo, String mobileCode, String sendType) {
		HashMap<String, Object> result = null;
		if ("sms".equals(sendType)) {
			ArrayList<String> mobileList = new ArrayList<String>();
			mobileList.add(mobileNo);
			result = SmsUtils.sendTemplateSms(mobileList, smsTemplateId, new String[] {mobileCode,"400-888-6633"});
		} else {
			result = SmsUtils.sendVoiceVerify(mobileCode, mobileNo, "4008886633", "3");
		}
		logger.info("手机号={}, 验证码={}, 短信平台返回：{}",mobileNo,mobileCode,result);
		if ("000000".equals(result.get("statusCode"))) {
			return true;
		} else {
			return false;
		}
	}
	
	
	public static boolean doSend4Invite(String mobileNo, String[] code, String sendType) {
		HashMap<String, Object> result = null;
		if ("reInviteCode".equals(sendType)) {		//注册生成邀请码短信通知
			ArrayList<String> mobileList = new ArrayList<String>();
			mobileList.add(mobileNo);
			result = SmsUtils.sendTemplateSms(mobileList, smsReInviteTeId, code);
		} else {			//老用户生成邀请码短信通知
			ArrayList<String> mobileList = new ArrayList<String>();
			mobileList.add(mobileNo);
			result = SmsUtils.sendTemplateSms(mobileList, smsInviteTeId, code);
		} 
		
		if ("000000".equals(result.get("statusCode"))) {
			return true;
		} else {
			return false;
		}
		
	}
	

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		HashMap<String, Object> result = null;
		SmsUtils smsUtils = new SmsUtils();
		try {

			smsUtils.init();
		} catch (Exception e) {
			e.printStackTrace();
		}

		ArrayList<String> mobileList = new ArrayList<String>();
		//		mobileList.add("18032607303");
		mobileList.add("18600405290");

		System.out.println(mobileList.toString().replaceAll("[\\]\\[\\s]", ""));

		//result = sendTemplateSms(mobileList,"1593" ,new String[]{DateUtil.getChineseFormatDate(),"123456"});
		result = sendVoiceVerify("123459", "18600405290", "4001516516", "3");

		if ("000000".equals(result.get("statusCode"))) {
			//正常返回输出data包体信息（map）
			HashMap<String, Object> data = (HashMap<String, Object>)result.get("data");
			Set<String> keySet = data.keySet();
			for (String key : keySet) {
				Object object = data.get(key);
				System.out.println(key + " = " + object);
			}
		} else {
			//异常返回输出错误码和错误信息
			System.out.println("错误码=" + result.get("statusCode") + " 错误信息= " + result.get("statusMsg"));
		}
	}

}
